package br.edu.fa7.quotes.model;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class QuoteDB {

	private static int counter = 0;

	private MeuHelper helper;
	protected SQLiteDatabase db;
	private static String SCRIPT_DATABASE_DELETE = "DROP TABLE IF EXISTS table_quote;";
	private static String[] SCRIPT_DATABASE_CREATE = new String[] {
			"create table table_quote(_id_quote integer primary key autoincrement, quote text not null, author text not null); ",
			"insert into table_quote(quote, author) values('Every little thing is gonna be alright.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Every little thing is gonna be alright.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('None but ourselves can free our minds.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('None but ourselves can free our minds.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Love the life you live. Live the life you love.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Love the life you live. Live the life you love.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('You never know how strong you really are until being strong is your only choice.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('You never know how strong you really are until being strong is your only choice.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('You ain`t gonna miss your water until your well runs dry.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('You ain`t gonna miss your water until your well runs dry.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('In this bright future you can`t forget your past.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('In this bright future you can`t forget your past.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Don`t gain the world and lose your soul, wisdom is better than silver or gold.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Don`t gain the world and lose your soul, wisdom is better than silver or gold.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('One good thing about music, when it hits, you feel no pain.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('One good thing about music, when it hits, you feel no pain.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Hit me with music.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Hit me with music.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('The truth is, everyone is going to hurt you. You just got to find the ones worth suffering for', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('The truth is, everyone is going to hurt you. You just got to find the ones worth suffering for', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Some people feel the rain. Others just get wet.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Some people feel the rain. Others just get wet.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Better to die fighting for freedom then be a prisoner all the days of your life.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Better to die fighting for freedom then be a prisoner all the days of your life.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Wake up and live.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Wake up and live.', 'Bob Marley'); ",
			"insert into table_quote(quote, author) values('Every man gotta right to decide his own destiny.', 'Bob Marley');",
			"insert into table_quote(quote, author) values('Every man gotta right to decide his own destiny.', 'Bob Marley');" };

	public QuoteDB(Context ctx) {
		helper = new MeuHelper(ctx, "db_quotes", 1,
				QuoteDB.SCRIPT_DATABASE_CREATE, QuoteDB.SCRIPT_DATABASE_DELETE);

		db = helper.getWritableDatabase();
	}

	public QuoteDB(MeuHelper mh) {
		helper = mh;
	}

	public Quote todaysQuote() {
		SQLiteDatabase db = helper.getReadableDatabase();
		counter = counter + 1;
		Cursor c = db.rawQuery(
				"select * from table_quote where _id_quote = "
				+ counter + " ;", null);

		if (c.getCount() > 0) {

			c.moveToFirst();
			Quote todaysQuote = new Quote();
			todaysQuote.setIdQuote(c.getLong(0));
			todaysQuote.setQuote(c.getString(1));
			todaysQuote.setAuthor(c.getString(2));

			return todaysQuote;

		} else {
			counter = 0;
			return null;
		}
	}

	public void fechar() {
		if (helper != null) {
			helper.close();
		}
	}

}
